// pages/member/coupon/index.js
var app = getApp()
var core = app.requirejs('core')
var api = require("../../../utils/api.js")
var image_url = 'https://totolestorage.blob.core.chinacloudapi.cn/totoleec/xcx/coupon/'
Page({
  data: {
    catelists: [{
      url: image_url + "tag-4-2.png",
      url_ico: image_url + "tag-4-1.png",
      coupons: image_url + "coupon-1.png",
      id: 0
    }, {
      url: image_url + "tag-5-2.png",
      url_ico: image_url + "tag-5-1.png",
      coupons: image_url + "coupon-2.png",
      id: 1
    }, {
      url: image_url + "tag-6-2.png",
      url_ico: image_url + "tag-6-1.png",
      coupons: image_url + "coupon-3.png",
      id: 2
    }],
    currentTab: 0,
    memberCoupon: [],
  },

  //生命周期函数--监听页面加载
  onLoad: function (options) {
    this.situationChange()
  },
  //监听页面显示
  onShow: function(){
    wx.hideHomeButton();
  },
  //切换tab
  tabNav: function (e) {
    let tabid = e.target.dataset.id
    this.setData({
      currentTab: tabid,
    })
    this.situationChange()
  },
  //当前用户优惠卷使用情况请求
  situationChange() {
    let $this = this
    let apicontent =api.Limit.selectMemberGiftDetail()
    core.post(apicontent, {
      globalId: app.getCache('userInfo').globalid,
      status: this.data.currentTab + 1,
    }, function (res) {
      $this.setData({
        memberCoupon: res.result
      })
    })
  },
  //加入购物车
  usecouponschange(e) {
    if(this.data.currentTab==1||this.data.currentTab==2){
      let text=this.data.currentTab==1?'礼包已使用':this.data.currentTab==2?'礼包已过期':''
      wx.showToast({
        title: text,
        icon: 'none',
        duration: 2000
      })
      return
    }
    let datas = e.currentTarget.dataset
    let goods1 = datas.goods1.split(':')
    let goods2 = datas.goods2.split(':')
      let add = {
        optionId: goods1[0],
        goodsId: goods1[1],
      }
      let add1 = {
        optionId: goods2[0],
        goodsId: goods2[1],
      }
      this.addShoppingcart(add)
      this.addShoppingcart(add1)
  },
  //添加购物车请求
  addShoppingcart(add) {
    let apicontent = api.orders.addCard()
    let goods1=this.data.memberCoupon[0].goods1
    let data = {
      total: '1',
      optionId: add.optionId,
      goodsId: add.goodsId,
      typeOption: '1',
      typeword: '礼包',
    }
    core.post(apicontent, {
      ...data
    }, function (ret) {
      if (ret.code == 1) {
        wx.reLaunch({
          url: '/pages/member/cart/index?goods='+goods1,
          success: function () {
            wx.showToast({
              icon: 'success',
              title: '添加购物车成功',
            });
          }
        })
      }
    })
  },
  navigate: function (e) {
    var phone = e.currentTarget.dataset.phone
    var appid = e.currentTarget.dataset.appid
    var appurl = e.currentTarget.dataset.appurl
    var url = e.currentTarget.dataset.url

    if (url.includes('?cate')) {
      let params = url.split('?')
      if (params[1].includes('cate')) {
        let cateid = params[1].split('=')[1]

        app.setCache("cateid", cateid)
      }
      wx.switchTab({
        url: url,
      })
    } else {
      wx.navigateTo({
        url: url,
        fail: function () {
          wx.switchTab({
            url: url,
          })
        }
      })
    }
    if (phone) {
      wx.makePhoneCall({
        phoneNumber: phone
      })
    }
    if (appid) {
      wx.navigateToMiniProgram({
        appId: appid,
        path: appurl
      })
    }
  },
  // 格式化时间
  timeFun(data) {
    var time = new Date(data)
    var y = time.getFullYear();
    var m = time.getMonth() + 1;
    var d = time.getDate();
    var h = time.getHours() < 10 ? '0' + time.getHours() : time.getHours();
    var mm = time.getMinutes() < 10 ? '0' + time.getMinutes() : time.getMinutes();
    var s = time.getSeconds() < 10 ? '0' + time.getSeconds() : time.getSeconds();
    return y + '.' + m + '.' + d + ' ' + h + ':' + mm + ':' + s;
  },
})